package com.sky.common.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 经纬海拔中心点计算工具类
 */
public class GeoCentroidCalculator {

    /**
     * 点位
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Point {
        double longitude;
        double latitude;
        double altitude;
    }


    /**
     * 将弧度转换为度
     * @param radians  弧度
     * @return 度
     */
    private static double radiansToDegrees(double radians) {
        return radians * 180.0 / Math.PI;
    }


    /**
     * 将度转换为弧度
     * @param degrees 度
     * @return 弧度
     */
    private static double degreesToRadians(double degrees) {
        return degrees * Math.PI / 180.0;
    }


    /**
     * 计算加权质心
      * @param locations 经纬海拔点位集合
     * @return 中心点
     */
    public static Point calculateCentroid(List<Point> locations) {
        double totalLongitudeRadians = 0;
        double totalLatitudeRadians = 0;
        double totalAltitude = 0;
        int count = locations.size();
        for (Point location : locations) {
            totalLongitudeRadians += degreesToRadians(location.longitude);
            totalLatitudeRadians += degreesToRadians(location.latitude);
            totalAltitude += location.altitude;
        }

        double centroidLongitudeRadians = totalLongitudeRadians / count;
        double centroidLatitudeRadians = totalLatitudeRadians / count;
        double centroidAltitude = totalAltitude / count;
        double centroidLongitude = radiansToDegrees(centroidLongitudeRadians);
        double centroidLatitude = radiansToDegrees(centroidLatitudeRadians);
        return new Point(centroidLongitude, centroidLatitude, centroidAltitude);
    }

}